<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <link href="/assets/accessibility.liveRegionAttachment.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" src="/__dist__/botframework-webchat-fluent-theme.production.min.js"></script>
    <style>
      .sendbox__error-message {
        all: unset !important;
        bottom: 100px !important;
        color: darkred !important;
        margin: 5px !important;
        position: absolute !important;
      }
    </style>
  </head>

  <body>
    <main id="webchat"></main>
    <script type="text/babel">
      run(async function () {
        const {
          React,
          ReactDOM: { render },
          WebChat: { FluentThemeProvider, ReactWebChat }
        } = window; // Imports in UMD fashion.

        const clock = lolex.createClock();

        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill: clock });

        // GIVEN: With a multiline text area.
        const App = () => (
          <ReactWebChat
            directLine={directLine}
            store={store}
            styleOptions={{
              sendBoxTextWrap: true,
              internalLiveRegionFadeAfter: 10000
            }}
            ponyfill={clock}
          />
        );

        render(
          <FluentThemeProvider>
            <App />
          </FluentThemeProvider>,
          document.getElementById('webchat')
        );

        await pageConditions.webChatRendered();

        clock.tick(600);

        await pageConditions.uiConnected();

        const errorMessage = document.querySelector('.sendbox__error-message');

        // GIVEN: The send box is empty.
        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxTextBox}"]`));
        await host.sendKeys('\n');
        clock.tick(50);

        // THEN: The message "Cannot send empty message." should appear.
        expect(errorMessage.innerText).toBe('Cannot send empty message.');
        await host.snapshot();

        // WHEN: Some text is typed into the send box
        clock.tick(50);
        await host.sendKeys('Hello, this is a multiline', '+SHIFT', '\n', '-SHIFT', 'message');

        // THEN: The error message should be empty
        clock.tick(50);
        expect(errorMessage.innerText).toBe('');

        // THEN: After 10000 ms, the status message should still be empty.
        clock.tick(10000);
        expect(errorMessage.innerText).toBe('');
        await host.snapshot();

        // WHEN: The message is sent
        await (await directLine.actPostActivity(() => host.sendKeys('\n'))).resolveAll();
        clock.tick(50);

        // THEN: Both error and status messages should be empty.
        expect(errorMessage.innerText).toBe('');
        await host.snapshot();
      });
    </script>
  </body>
</html>
